package com.hui.kafka;

import com.hui.entity.Product;
import com.hui.repository.ProductRepository;
import com.hui.service.ProductService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/09/26/9:22
 * @Description: kafka消费者
 */
@Component
public class Consumer {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductRepository repository;

    @KafkaListener(topics = "product")
    public void listen(ConsumerRecord record) {
        Object value =  record.value();
        // 获取id，数据库查询
        Product product = productService.getById(Integer.parseInt(value.toString()));
        System.out.println(product);
        // 将查询到的数据保存到ElasticSearch中
        repository.save(product);
    }

}
